/**
 * @class   wosh::ResourcePath
 * @brief   
 * 
 *
 *
 *
 ****************************************************************************
 * @version $Id: ResourcePath.h 2863 2010-08-07 04:28:58Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR UserS;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_ResourcePath_H__
 #define __WOSH_Core_ResourcePath_H__

 #include <woshDefs.h>
 #include <core/Serialization.h>
 #include <string>



namespace wosh {

 #define _ResourcePath_ProtocolTerminator	"://"
 
 #define _ResourcePath_ProtocolDefault		"file"
 #define _ResourcePath_ProtocolFile			"file"


class ResourcePath : public virtual wosh::ISerializable
 {
	WOSH_SERIALIZABLE(wosh::ResourcePath)
	WOSH_COMPARE_RTTI(wosh::ResourcePath)

	public:
		/**
		 * @brief  Empty constructor.
		 */
		ResourcePath() { }
		/**
		 * @brief  Empty constructor with default initializer.
		 * @param [in] fullpath the full path including protocol
		 *         (such as http://wosh.sourceforge.net/ or file:://$ROOT/README)
		 * @see    set()
		 */
		ResourcePath( const std::string& fullpath );
		/**
		 * @brief  Empty constructor with default initializer.
		 * @param [in] path the path without protocol (such as wosh.sourceforge.net or $ROOT/README)
		 * @param [in] protocol the protocol to be set (such as 'http' or 'file')
		 * @see    setPath(), setProtocol()
		 */
		ResourcePath( const std::string& path, const std::string& protocol );
		/**
		 * @brief  Copy constructor.
		 * @param m [in] the source object to be copied
		 */
		ResourcePath( const ResourcePath& m );
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~ResourcePath() { }


	public:
		/**
		 * @brief  Get the resolved path of the resource. When the resource is a File
		 *         it will be resolved to local file.
		 * @param [in] omit_protocol_prefix when true the protocol is omitted, else it is included
		 * @return the literal string representing the protocol (suffix '://' is omitted)
		 * @see    FilePath::resolve(), FilePath::toLocalPath()
		 */
		virtual std::string resolve( bool omit_protocol_prefix ) const;

/** @name Getters
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief  Get the protocol of the resource. Common protocols are 'http', 'file' and so on.
		 *         Full path is formatted as follow: protocol://path
		 * @return the literal string representing the protocol (suffix '://' is omitted)
		 */
		virtual const std::string& getProtocol() const		{ return this->protocol; }
		virtual std::string& getProtocol()					{ return this->protocol; }

		/**
		 * @brief  Get the path of the resource. Protocol is omitted.
		 *         Full path is formatted as follow: protocol://path
		 * @return the literal string representing the path
		 */
		virtual const std::string& getPath() const			{ return this->path; }
		virtual std::string& getPath()						{ return this->path; }

		/**
		 * @brief  Get the full path of the resource, including protocol.
		 *         Full path is formatted as follow: protocol://path
		 * @return the literal string representing the full path (separator '://' is included)
		 */
		virtual std::string getPathFull() const {
			if ( this->protocol.empty() )
				return std::string(_ResourcePath_ProtocolFile) + _ResourcePath_ProtocolTerminator + this->path;
			return this->protocol + _ResourcePath_ProtocolTerminator + this->path;
		 }

		/**
		 * @brief  Evaluate if the resource is a file resource.
		 * @return true if protocol is 'file' or empty; false else.
		 */
		inline bool isFile() const							{ return (this->protocol.empty() || this->protocol == _ResourcePath_ProtocolFile); }

		/**
		 * @brief  Clear the internal fiedls.
		 */
		virtual void clear() {
			this->protocol.clear();
			this->path.clear();
		 }

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief  Set the protocol of the resource. Common protocols are 'http', 'file' and so on.
		 *         Full path is formatted as follow: protocol://path
		 * @param [in] value the literal string representing the protocol (leave out suffix '://')
		 */
		virtual void setProtocol( const std::string& value )	{ this->protocol = value; }

		/**
		 * @brief  Set the path of the resource. Protocol is unchanged.
		 *         Full path is formatted as follow: protocol://path
		 * @param [in] value the literal string representing the path
		 */
		virtual void setPath( const std::string& value )		{ this->path = value; }

		/**
		 * @brief  Set the full path of the resource, including protocol.
		 *         Full path is formatted as follow: protocol://path
		 * @param [in] full_path the literal string representing the full path (include '://')
		 * @return false if the argument is empty; true else.
		 */
		virtual bool set( const std::string& full_path );

//@}
/** @name Operators Overloading
 * @{
 ******************************************************************************/
	public:
		virtual ResourcePath& operator=(const ResourcePath& m);
		virtual bool operator==(const ResourcePath &other) const;
		virtual bool operator!=(const ResourcePath &other) const	{ return !(*this == other);  }
//@}
/** @name Serialization interface
 * @{
 ******************************************************************************/
	public:
		virtual inline std::string toString() const		{ return getPathFull(); }
		virtual bool updateHash( DataHashContext* hash_context ) const;
//@}
/** @name Internal vars
 * @{
 ******************************************************************************/
	protected:
		std::string protocol;	///< access protocol (http, ftp, file, ..), '://' is omitted
		std::string path;		///< the platform independent path (WOSH syntax)

//@}


}; // class def

}; // namespace wosh

#endif //__WOSH_Core_ResourcePath_H__
